package com.dftdla.server.message;

import com.alibaba.fastjson2.JSON;
import com.dftdla.server.message.request.*;
import com.dftdla.server.message.response.LoginResponseMessage;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 14501
 */
@Data
public abstract class Message implements Serializable {

    /**
     * 根据消息类型字节，获得对应的消息 class
     * @param messageType 消息类型字节
     * @return 消息 class
     */
    public static Class<? extends Message> getMessageClass(int messageType) {
        return MESSAGE_CLASSES.get(messageType);
    }

    private Long userId;

    private int messageType;

    private String token;

    public abstract int getMessageType();

    public static final int LOGIN_REQUEST_MESSAGE = 0;
    public static final int CHAT_REQUEST_MESSAGE = 1;
    public static final int GROUP_CREATE_REQUEST_MESSAGE = 2;
    public static final int GROUP_JOIN_REQUEST_MESSAGE = 3;
    public static final int GROUP_QUIT_REQUEST_MESSAGE = 4;
    public static final int GROUP_CHAT_REQUEST_MESSAGE = 5;
    public static final int GROUP_MEMBERS_REQUEST_MESSAGE = 6;
    public static final int LOGIN_RESPONSE_MESSAGE = 7;

    private static final Map<Integer, Class<? extends Message>> MESSAGE_CLASSES = new HashMap<>();

    static {
        MESSAGE_CLASSES.put(LOGIN_REQUEST_MESSAGE, LoginRequestMessage.class);
        MESSAGE_CLASSES.put(CHAT_REQUEST_MESSAGE, ChatRequestMessage.class);
        MESSAGE_CLASSES.put(GROUP_CREATE_REQUEST_MESSAGE, GroupCreateRequestMessage.class);
        MESSAGE_CLASSES.put(GROUP_JOIN_REQUEST_MESSAGE, GroupJoinRequestMessage.class);
        MESSAGE_CLASSES.put(GROUP_QUIT_REQUEST_MESSAGE, GroupQuitRequestMessage.class);
        MESSAGE_CLASSES.put(GROUP_CHAT_REQUEST_MESSAGE, GroupChatRequestMessage.class);
        MESSAGE_CLASSES.put(GROUP_MEMBERS_REQUEST_MESSAGE, GroupMembersRequestMessage.class);
        MESSAGE_CLASSES.put(LOGIN_RESPONSE_MESSAGE, LoginResponseMessage.class);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

}
